/**
 * Copyright (c) 2009  Collaborative Development Group, C.S. Dept., University of Bari
 *
 * All rights reserved. This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0  which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 */
package it.uniba.di.cdg.penelope.presenterfirst;

import java.beans.PropertyChangeEvent;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.TypeSafeMatcher;

/**
 * Matches the {@link PropertyChangeEvent} values.
 */
public class PropertyMatcher extends TypeSafeMatcher<PropertyChangeEvent> {

	private final String propertyName;
	private final Object oldValue;
	private final Object newValue;


	/**
	 * @param propertyName
	 * @param oldValue
	 * @param newValue
	 */
	public PropertyMatcher( String propertyName, Object oldValue, Object newValue ) {
		this.propertyName = propertyName;
		this.oldValue = oldValue;
		this.newValue = newValue;
	}

	/* (non-Javadoc)
	 * @see org.hamcrest.TypeSafeMatcher#matchesSafely(java.lang.Object)
	 */
	@Override
	public boolean matchesSafely( PropertyChangeEvent item ) {
		return propertyName.equals( item.getPropertyName() ) &&
			oldValue.equals( oldValue ) &&
			newValue.equals( newValue );
	}

	/* (non-Javadoc)
	 * @see org.hamcrest.SelfDescribing#describeTo(org.hamcrest.Description)
	 */
	@Override
	public void describeTo( Description description ) {
		description.appendText( "Property value do not match" );
	}

	@Factory
	public static PropertyMatcher isProperty( String propertyName, Object oldValue, Object newValue ) {
		return new PropertyMatcher( propertyName, oldValue, newValue );
	}
}
